//
//  MLWLXPersonTableViewCell.m
//  KITCHEN_STORIES
//
//  Created by dllo on 16/6/15.
//  Copyright © 2016年 MLW. All rights reserved.
//

#import "MLWLXPersonTableViewCell.h"
#import "UIImageView+WebCache.h"

#import "MLWLXSmallPersonModel.h"

@implementation MLWLXPersonTableViewCell


- (void)dealloc
{
    [self.scrollView release];
    [self.lxView release];
    [super dealloc];
}
- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}



    
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    
    if (self) {
        
          self.scrollDataSource = [NSMutableArray array];

  
        
        self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight / 4)];
        
        self.scrollView.showsHorizontalScrollIndicator = NO;
        //签代理:
        self.scrollView.delegate = self;
        //整页翻动:
        self.scrollView.pagingEnabled = YES;

        
        [self.contentView addSubview:_scrollView];
        [_scrollView release];
        
        
        self.lxView = [[UIView alloc] initWithFrame:CGRectMake(0,self.scrollView.frame.size.height - 25, ScreenWidth, 25)];
        self.lxView.backgroundColor = [UIColor colorWithRed:0.14 green:0.14 blue:0.14 alpha:0.4];
        [self.contentView addSubview:_lxView];
        [_lxView release];

    }
    
    return self;
}
// 每次运行时布局子视图
- (void)layoutSubviews
{
    [super layoutSubviews];
    self.lxView.backgroundColor = [UIColor colorWithRed:0.14 green:0.14 blue:0.14 alpha:0.4];
    
    
}

#pragma mark -- 重写datasource的set方法:
- (void)setScrollDataSource:(NSMutableArray *)scrollDataSource
{
    if (_scrollDataSource != scrollDataSource) {
        [_scrollDataSource release];
        _scrollDataSource = [scrollDataSource retain];
        
    }
    
    self.scrollView.contentSize = CGSizeMake(self.scrollDataSource.count * ScreenWidth, 0);
    
    for (NSInteger i = 0; i < self.scrollDataSource.count; i++) {
        self.lxLabel = [[UILabel alloc] initWithFrame:CGRectMake(10 + i * ((ScreenWidth - 20) /self.scrollDataSource.count), 5, (ScreenWidth - 20) /self.scrollDataSource.count, 15)];
     MLWLXSmallPersonModel *smodel = [_scrollDataSource objectAtIndex:i];
        
        self.lxLabel.text = smodel.CateName;
        
         self.arrayNN = [NSMutableArray array];
        [self.arrayNN addObject:smodel.CateName];
        [smodel.CateName release];
        
        self.lxLabel.textColor = [UIColor whiteColor];
        [self.lxView addSubview:_lxLabel];
        [_lxLabel release];
        
        if (i == 0) {
            self.lxLabel.textColor = [UIColor redColor];
        }
        
    }
    
        //**     --------  加入图片数据        --------*/
    for (NSInteger i = 0; i < self.scrollDataSource.count; i++) {
  
        //设置偏移量:
        self.scrollView.contentOffset = CGPointMake(0, 0);
        
        
        self.imageViewgungun = [[UIImageView alloc] initWithFrame:CGRectMake(ScreenWidth * i, 0, ScreenWidth, ScreenHeight / 3)];
        
        MLWLXSmallPersonModel *smodel = [_scrollDataSource objectAtIndex:i];
        
        [self.imageViewgungun sd_setImageWithURL:[NSURL URLWithString:smodel.Cover] placeholderImage:[UIImage imageNamed:@"placeholder"]];
        
      
        [self.scrollView addSubview:_imageViewgungun];
        [_imageViewgungun release];
        
    }
    
}

//停止滚动时触发:
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    
    
    CGFloat num = self.scrollView.contentOffset.x;
    NSInteger uuu = num / ScreenWidth;
    
    UILabel *label = [[UILabel alloc] init];
    

    for (NSInteger i = 0; i < [[self.lxView subviews] count]; i++) {

        if (uuu == i) {

            
            label = [[self.lxView subviews] objectAtIndex:i];
            
            label.textColor = [UIColor redColor];
            
        }
       
        else
        {

            label = [[self.lxView subviews] objectAtIndex:i];
            
            label.textColor = [UIColor whiteColor];
        }

    }
    
    
    
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
